<template>
  <j-modal
    :title="title"
    width="800px"
    :visible="visible"
    switchFullscreen
    centered
    @cancel="onCancel"
    @ok="onOk"
    cancelText="关闭"
    :footer="footer"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="缩略图" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="file">
              <!-- <j-image-upload isMultiple v-model="model.thumb" :number="1"></j-image-upload> -->
              <JImageUploadLed isMultiple v-model="model.file" :number="1" @uploadSuccess="uploadSuccess" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage';
import JImageUploadLed from '@/components/jeecg/JImageUpload_led'
export default {
  name: 'ImageUploadModal',
  components: { JImageUploadLed },
  data() {
    return {
      title: '上传',
      confirmLoading: false,
      visible: false,
      model: {},
      validatorRules: {
        file: [{ required: true, message: '请上传图片!' }],
      },
      url: {
        add: '/corp/tbFile/upload',
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      }
    }
  },
  methods: {
    init() {
      this.visible = true;
    },
    onOk() {
      const that = this
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          httpAction(that.url.add, this.model, 'post')
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                this.visible = false
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    footer() {
      return <a-button onClick={this.onCancel}>关闭</a-button>
    },
    uploadSuccess() {
      this.$emit('uploadSuccess')
      this.visible = false
    },
    onCancel() {
      this.visible = false
      this.$refs.form.resetFields()
    },
  },
}
</script>

<style lang="less" scoped>
</style>